package dataProviders;

public class GenerateSQL {

	private String statement = "";

	public void addTable(String tableName) {
		statement += "\nCREATE  TABLE " + tableName
				+ " (pid_" + tableName + " INT NOT NULL ,PRIMARY KEY (pid_"
				+ tableName + ") );";
	}
	public void addProperty(String tableName,String name, String type){
		statement+="\nALTER TABLE "+tableName+" ADD COLUMN "+name+" "+type+";";
	}
	
	public void addLink(String sourceTableName, String targetTableName, int multiplicity){
		if(multiplicity==0){
			statement+="\nALTER TABLE "+sourceTableName+" ADD COLUMN fid_"+targetTableName+" int not null;";
			statement+="\nALTER TABLE "+sourceTableName+" ADD UNIQUE KEY fk_"+targetTableName+" (fid_"+targetTableName+");";
			statement+="\nALTER TABLE "+sourceTableName+" ADD FOREIGN KEY (fid_"+targetTableName+") REFERENCES "+targetTableName+ "(pid_"+targetTableName+");";
			return;
		}
		if(multiplicity==3){
			String linkTable = sourceTableName+"_"+targetTableName;
			statement+="\nCREATE TABLE "+linkTable+"(id int not null, primary key (id));";
			statement+="\nALTER TABLE "+linkTable+" ADD COLUMN fid_"+sourceTableName+" int not null;";
			statement+="\nALTER TABLE "+linkTable+" ADD UNIQUE KEY fk_"+sourceTableName+" (fid_"+sourceTableName+");";
			statement+="\nALTER TABLE "+linkTable+" ADD FOREIGN KEY (fid_"+sourceTableName+") REFERENCES "+sourceTableName+ "(pid_"+sourceTableName+");";
			statement+="\nALTER TABLE "+linkTable+" ADD COLUMN fid_"+targetTableName+" int not null;";
			statement+="\nALTER TABLE "+linkTable+" ADD UNIQUE KEY fk_"+targetTableName+" (fid_"+targetTableName+");";
			statement+="\nALTER TABLE "+linkTable+" ADD FOREIGN KEY (fid_"+targetTableName+") REFERENCES "+targetTableName+ "(pid_"+targetTableName+");";
			return;
		}
		if(multiplicity==1){
			String temp;
			temp = sourceTableName;
			sourceTableName = targetTableName;
			targetTableName = temp;
		}
		statement+="\nALTER TABLE "+sourceTableName+" ADD COLUMN fid_"+targetTableName+" int not null;";
		statement+="\nALTER TABLE "+sourceTableName+" ADD FOREIGN KEY (fid_"+targetTableName+") REFERENCES "+targetTableName+ "(pid_"+targetTableName+");";
		return;
	}

	@Override
	public String toString() {
		return statement;
	}
	public void clearStatement(){
		statement="";
	}
}
